package com.chuhe.starry.controller;


import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.chuhe.common.constans.MyConstants;
import com.chuhe.common.domain.dto.TokenUserInfoDto;
import com.chuhe.common.domain.entity.UserInfo;
import com.chuhe.common.domain.vo.req.SaveUserInfoReq;
import com.chuhe.common.domain.vo.resp.ApiResult;
import com.chuhe.common.domain.vo.resp.GetUserInfoResp;
import com.chuhe.common.utils.RedisComponet;
import com.chuhe.starry.service.UserInfoService;
import com.chuhe.starry.websocket.ChannelContextUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import java.io.IOException;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author <a href="张楚河">chuhe_z</a>
 * @since 2025-05-08
 */
@RestController
@RequestMapping("/userInfo")
@Api(value = "用户相关接口", tags = "用户相关API", description = "用户相关API")
public class UserInfoController {
    @Autowired
    private RedisComponet redisComponet;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ChannelContextUtils channelContextUtils;

    @RequestMapping("/getUserInfo")
    @ApiOperation("获取用户信息")
    public ApiResult<GetUserInfoResp> getUserInfo(HttpServletRequest request){
        TokenUserInfoDto userInfoDto = redisComponet.getTokenUserInfoByToken(request);
        return ApiResult.success(userInfoService.getUserInfo(userInfoDto));
    }

    @RequestMapping("/saveUserInfo")
    @ApiOperation("保存用户信息")
    public ApiResult<?> saveUserInfo(HttpServletRequest request,
                                     UserInfo userInfo,
                                     MultipartFile avatarFile,
                                     MultipartFile avatarCover) throws IOException {
        userInfoService.saveUserInfo(request,userInfo,avatarFile,avatarCover);
        return getUserInfo(request);
    }

    @RequestMapping("/updatePassword")
    @ApiOperation("修改密码")
    public ApiResult<?> updatePassword(HttpServletRequest request, @NotEmpty String password){
        TokenUserInfoDto userInfoDto = redisComponet.getTokenUserInfoByToken(request);
        userInfoService.updatePassword(userInfoDto,password);
        // todo 修改完密码断线
        channelContextUtils.closeContext(userInfoDto.getUserId());
        return ApiResult.success(null);
    }

    @RequestMapping("/logout")
    @ApiOperation("下线")
    public ApiResult<?> logout(HttpServletRequest request){
        TokenUserInfoDto userInfoDto = redisComponet.getTokenUserInfoByToken(request);
        // todo 关闭 ws通道 下线
        channelContextUtils.closeContext(userInfoDto.getUserId());
        return ApiResult.success(null);
    }


}

